<?php

namespace App\Admin\Controllers;

use App\Models\ShopRole;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;


class ShopRoleController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '竞选列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ShopRole);

        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('姓名'))->expand(function ($model) {

            $datas = $model->where('id',$this->id)->get()->map(function ($data,$key) {     
                $data->key = $key+1;
                return $data->only(['key','address','self_introduction']);;
            });
            // dd($datas);
            
            return new Table(['序号', '所在区域','自我介绍'], $datas->toArray());
        });
;

        $grid->shop()->shop_name(__('店铺名称'));
        $grid->column('role', __('角色'))->display(function($value){
            switch ($value) {
                case '1':
                    return "<span class='label label-warning'>店长</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>店员</span>";
                    break;
                case '3':
                    return "<span class='label label-primary'>模特</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->column('hot_spot', __('点赞数'));
       
        $grid->column('phone', __('手机号'));
        $grid->column('img_url', __('个人照片'))->gallery(['width' => 50, 'height' => 50]);
        $grid->column('created_at', __('申请时间'));

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->where(function ($query){
                $query->whereHas('shop',function ($query){
                    $query->where('shop_name','like',$this->input);
                });
            },'店铺名称');
            $filter->like('phone', '手机号');
            // 在这里添加字段过滤器
            $filter->between('created_at', '申请时间')->datetime();
            $filter->scope('role1', '店长')->where('role', 1);
            $filter->scope('role2', '店员')->where('role', 2);
            $filter->scope('role3', '模特')->where('role', 3);

        });

        $grid->disableActions();
        $grid->disableCreateButton();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ShopRole::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('shop_id', __('Shop id'));
        $show->field('role', __('Role'));
        $show->field('hot_spot', __('Hot spot'));
        $show->field('name', __('Name'));
        $show->field('phone', __('Phone'));
        $show->field('area', __('Area'));
        $show->field('self_introduction', __('Self introduction'));
        $show->field('img_url', __('Img url'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ShopRole);

        $form->number('shop_id', __('Shop id'));
        $form->switch('role', __('Role'));
        $form->number('hot_spot', __('Hot spot'));
        $form->text('name', __('Name'));
        $form->mobile('phone', __('Phone'));
        $form->text('area', __('Area'));
        $form->text('self_introduction', __('Self introduction'));
        $form->multipleImage('img_url', '图片');

        return $form;
    }
}
